package com.mall.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mall.ibatis.bean.Function;
import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.Operator;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.FunctionDAO;
import com.ibatis.dao.client.DaoManager;

/**
 * <p/>
 * Date: Mar 6, 2004 11:22:43 PM
 * 
 * @author Clinton Begin
 */
public class FunctionService {

  /* Constants */

  private static final FunctionService instance = new FunctionService();

  /* Private Fields */

  private DaoManager daoManager = DaoConfig.getDaomanager();

  private FunctionDAO functionDAO;

  /* Constructors */

  public FunctionService() {
	  functionDAO = (FunctionDAO) daoManager.getDao(FunctionDAO.class);
  }

  /* Public Methods */

  public static FunctionService getInstance() {
    return instance;
  }

  /* Function */

 public List<Function> selectFunctions(String functionParentID)
 {
	 return functionDAO.selectFunction(functionParentID);
 }
 
 
	 public List<Function> selectFunctionsRights(String ParentID,String or_operator_id){
		 List<Function> list = functionDAO.selectFunctionsRights(ParentID,or_operator_id);
//			Collections.sort(list,new Comparator<Function>(){
//				public int compare(Function s1,Function s2){
//					return s1.getFunctionOrder().compareTo(s2.getFunctionOrder());
//				}
//			});
		 return list;
	 }
	 
	 //方法过滤查询
	 public void methodFilter(HttpServletRequest request,HttpServletResponse response){
		HttpSession session = request.getSession();
		Operator op = (Operator) session.getAttribute("operator");
		String userId = op.getOpID();
		String menuId = request.getParameter("functionId");
		List<String> list = functionDAO.methodFilter(menuId, userId);
		List<Method> methods = functionDAO.methodAllFilter(menuId);
		for(Method m : methods){
			if(list.contains(m.getId())){
				m.setNum2(m.getNum());
			}else{
				m.setNum2("");
			}
		}
		request.setAttribute("list",methods);
		request.setAttribute("response",response);
	 }
	 /**
	  * 
	  */
  
}
